package edu.tapestry.iddb.client.pages.groupIM;

import iddb.AbstractResponse;
import iddb.GroupPOJO;
import iddb.OkResponse;
import iddb.PluginIdentificator;

import org.apache.tapestry5.annotations.ApplicationState;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Property;

import edu.tapestry.iddb.client.pages.AbstrPage;
import edu.tapestry.iddb.client.pages.ErrorPage;
import edu.tapestry.iddb.client.pages.OKPage;
import edu.tapestry.iddb.client.webService.CommandsGenerator;
import edu.tapestry.iddb.client.webService.IDDBProxy;

public class AddGroupIM extends AbstrPage{
	@Property
	private GroupPOJO group;

	@ApplicationState
	private IDDBProxy proxy;

	@InjectPage
	private OKPage ok;

	@InjectPage
	private ErrorPage error;
	
	private PluginIdentificator ident;
	
	public Object initialize(PluginIdentificator ident){
		this.ident = ident;
		return this;
	}
	
	
	Object onActionFromAdd(){
		AbstractResponse resp = proxy.execute(CommandsGenerator.createAddGroup(group.getName(), this.ident));
		
		if (resp instanceof OkResponse){
			return ok;
		}
		else {
			return error.initialize(resp);
		}
	}
}
